/******************************************************************************************
 * EGR 330 - Power System Term Project
 * Home Power Meter
 * Using an MBed and a 3.3V Serial Enabled LCD
 *
 *
 *
 ******************************************************************************************/
//HEADER FILES
#include "mbed.h"
#include "LCD_Display.h"
#include "TextLCD.h"
#include "voltage.h"
#include "phase_Angle.h"
#include "current.h"
#include "power.h"
#include "power_Factor.h"
#include "SD_Storage.h"
/*******************************************************************
 *
 *
 *******************************************************************/
int main() {
    // Initization of variables. 
    float voltage = 0;
    float current_1 = 0;
    float rms_voltage = 0;
    float voltage_peak = 0;
    float current_1_time = 0;
    float voltage_time_start = 0;
    float power_1 = 0;
    float power_Factor_1 = 0;
    float current_peak_1 = 0;
    float phase_angle_1 = 0;
    double count =0;
    bool run = true;
    Timer time;
    
    FILE *fp = fopen("/sd/powerMeter.csv", "w");

    while(run){
        // Voltage Monitoring Method
        voltage = voltage_Monitoring();
        // Calculates rms voltage Method
        rms_voltage = calcuate_RMS_Voltage(voltage);
        // Is at Peak?
        voltage_peak = is_Voltage_at_Peak(voltage,time);
        // Start time
        voltage_time_start = time.read();
        // Current Monitoring Method
        current_1 = current_Monitoring(1);
        // Is at Peak?
        current_peak_1 = is_Current_Sensor_at_Peak(current_1,time); 
        current_1_time = current_time_is(1);
       /* current_2 = current_Monitoring(2);
        // Is at Peak?
        current_peak_2 = is_Current_Sensor_at_Peak(current_2,time); 
        current_2_time = current_time_is(2);
        current_3 = current_Monitoring(3);
        // Is at Peak?
        current_peak_3 = is_Current_Sensor_at_Peak(current_3,time);
        current_3_time = current_time_is(3);
        current_4 = current_Monitoring(4);
        // Is at Peak?
        current_peak_4 = is_Current_Sensor_at_Peak(current_4,time);
        current_4_time = current_time_is(4);
        current_5 = current_Monitoring(5);
        // Is at Peak? 
        current_peak_5 = is_Current_Sensor_at_Peak(current_5,time);
        current_5_time = current_time_is(5); */
        
        if(current_peak_1!=-100000){    
            phase_angle_1 = calculate_Phase_Angle(current_1_time);
            power_Factor_1 = calculate_Power_Factor(phase_angle_1);
            power_1 = calculate_power(current_1,rms_voltage,power_Factor_1);
            //Writes data to SD card.
            write_to_SD_Storage(1, rms_voltage, current_1, power_1 , phase_angle_1, power_Factor_1);
        }
        /*if(current_peak_2!=-100000){    
            phase_angle_2 = calculate_Phase_Angle(current_2_time);
            power_Factor_2 = calculate_Power_Factor(phase_angle_2);
            power_2 = calculate_power(current_2,rms_voltage,power_Factor_2);
            //Writes data to SD card.
            write_to_SD_Storage(2, rms_voltage, current_2, power_2 , phase_angle_2, power_Factor_2);
        }
        if(current_peak_3!=-100000){    
            phase_angle_3 = calculate_Phase_Angle(current_3_time);
            power_Factor_3 = calculate_Power_Factor(phase_angle_3);
            power_3 = calculate_power(current_3,rms_voltage,power_Factor_3);
            //Writes data to SD card.
            write_to_SD_Storage(3, rms_voltage, current_3, power_3 , phase_angle_3, power_Factor_3);
        }
        if(current_peak_4!=-100000){    
            phase_angle_4 = calculate_Phase_Angle(current_4_time);
            power_Factor_4 = calculate_Power_Factor(phase_angle_4);
            power_4 = calculate_power(current_4,rms_voltage,power_Factor_4);
            //Writes data to SD card.
            write_to_SD_Storage(4, rms_voltage, current_4, power_4 , phase_angle_4, power_Factor_4);
        }
        if(current_peak_5!=-100000){      
            phase_angle_5 = calculate_Phase_Angle(current_5_time);
            power_Factor_5 = calculate_Power_Factor(phase_angle_5);
            power_5 = calculate_power(current_5,rms_voltage,power_Factor_5);
            //Writes data to SD card.
            write_to_SD_Storage(5, rms_voltage, current_5, power_5 , phase_angle_5, power_Factor_5);
        }*/

       // Dispaly_LCD(Circuit Number, Power, Phase Angle, Power factor)
        if(count <100){
            display_LCD(1,power_1,phase_angle_1,power_Factor_1);
        }else if (count >100 &&count <200){
            //display_LCD(2,power_2,phase_angle_2,power_Factor_2);
        }else if (count>200 && count<300){  
            // display_LCD(3,power_3,phase_angle_3,power_Factor_3);
        }else if(count>300 && count <400){ 
            // display_LCD(4,power_4,phase_angle_4,power_Factor_4);
        }else{
            // display_LCD(5,power_5,phase_angle_5,power_Factor_5);
            count =0;
        }
        count ++;
        timer_end(time);
        
    }
}

